package linter.controller.Reviewed;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DO.User;
import linter.POJO.DTO.PostsDTO;
import linter.POJO.DTO.UserDTO;
import linter.POJO.DTO.classDataUploaderDTO;
import linter.custom.ResultVO;
import linter.service.ClassDataService;
import linter.service.PostsService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

import static linter.custom.CustomResultType.FILE_NOT_EXIST;
import static linter.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@Api(tags = "管理员控制器")
@RestController
@RequestMapping("/Reviewed")
public class ReviewedPeopleController {
    @Resource
    UserService userService;
    @Resource
    PostsService postsService;
    @Resource
    ClassDataService classDataService;





    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return ResultVO.success(userDTO);
    }
    @PostMapping("/allClass")
    @ApiOperation("管理员获取所有的课程")
    public ResultVO<PageInfo<classDataUploaderDTO>> allClass(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(classDataService.AllClass(pageNum)); }
        return ResultVO.error(USER_NOT_EXIST);
    }
    @PostMapping("/ban")
    @ApiOperation("用户封禁账号")
    public ResultVO<String> ban(@RequestParam(value = "userId") int id,@RequestParam(value = "state") int state){
         String message = userService.ban(id,state);
         return ResultVO.success(message);
    }
    @PostMapping("/updateUser")
    @ApiOperation("用户信息的修改")
    public ResultVO<String> updateUser(@RequestBody User user){
         if(userService.updateById(user)){
             return ResultVO.success("用户信息修改成功");
        }
         return ResultVO.error("系统内部错误");
    }

    @PostMapping("/BlogReviewed")
    @ApiOperation("文章审核")
    public ResultVO<String> blogReviewed(@RequestParam Integer postsId, @RequestParam Integer reviewedStatus){
            return postsService.reviewedPost(postsId, reviewedStatus);
    }


    @PostMapping("/getReviewedPosts")
    @ApiOperation("获取审核文章")
    public ResultVO<PageInfo<PostsDTO>> getReviewedPosts(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        if (Objects.equals(username, "Admain")){
            return postsService.getReviewedPosts(pageNum, pageSize);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/uploadClass")
    @ApiOperation("上传课程")
    public ResultVO<String> uploadClass(@RequestBody classDataUploaderDTO classDataUploaderDTO){
        return null;
//            return ResultVO.success(classDataService.uploadClass(classDataDTO));
    }

    @PostMapping("/uploadClassImage")
    @ApiOperation("修改上传课程的封面")
    public ResultVO<String> uploadClassImage(@RequestParam("file") MultipartFile file,
                                             String classType, String className, Long classId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        if (Objects.equals(username, "Admain")){
            if (file != null && !file.isEmpty() ){
                return  ResultVO.success(classDataService.uploadClassImage(file, classType, className, classId));
            }
            return ResultVO.error(FILE_NOT_EXIST);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }
}
